package com.yanqu.road.server.logic.activity.orewar.condition;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.enums.activity.eActivityOreWarType;
import com.yanqu.road.entity.event.EventTriggerArgs;
import com.yanqu.road.entity.event.IEventListener;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.orewar.OreWarActivityModule;
import com.yanqu.road.server.logic.activity.orewar.OreWarActivityData;
import com.yanqu.road.server.logic.activity.orewar.condition.union.CrossOreWarActivityScoreUnionRankCondition;
import com.yanqu.road.server.logic.activity.orewar.condition.union.OreWarActivityScoreUnionRankCondition;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;

import java.math.BigInteger;

public abstract class BaseOreWarActivityCondition {
    protected GamePlayer player;
    protected OreWarActivityData activityData;
    protected ActivityConditionInfo conditionInfo;

    public BaseOreWarActivityCondition(GamePlayer player, OreWarActivityData activityData, ActivityConditionInfo conditionInfo) {
        this.player = player;
        this.activityData = activityData;
        this.conditionInfo = conditionInfo;
    }

    public abstract void addTrigger(GamePlayer player);

    public abstract void removeTrigger(GamePlayer player);

    public abstract boolean isCompleted(GamePlayer player);

    public static BaseOreWarActivityCondition createActivityCondition(GamePlayer player, OreWarActivityData activityData, ActivityInfo activityInfo, ActivityConditionInfo conditionInfo) {
        if(activityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
            switch (conditionInfo.getType()) {
                case OreWarActivityMgr.CONDITION_TYPE_USER:
                    return new OreWarActivityScoreRankCondition(player, activityData, conditionInfo);
                case OreWarActivityMgr.CONDITION_TYPE_UNION:
                    return new OreWarActivityScoreUnionRankCondition(player, activityData, conditionInfo);
            }
        }else if(activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
            switch (conditionInfo.getType()) {
                case OreWarActivityMgr.CONDITION_TYPE_USER:
                    return new CrossOreWarActivityScoreRankCondition(player, activityData, conditionInfo);
                case OreWarActivityMgr.CONDITION_TYPE_UNION:
                    return new CrossOreWarActivityScoreUnionRankCondition(player, activityData, conditionInfo);
            }
        }
        return null;
    }

    public final void setValue(BigInteger value) {
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if(null != userActivityConditionData){
            userActivityConditionData.setValue(value);
        }
        player.getModule(OreWarActivityModule.class).onActivityDataChange(activityData);
    }

    public  BigInteger getPreValue() {
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if(null != userActivityConditionData){
            return userActivityConditionData.getPreValue();
        }else {
            return BigInteger.ZERO;
        }
    }

    public final BigInteger getValue() {
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if(null != userActivityConditionData){
            return userActivityConditionData.getValue();
        }
        return BigInteger.ZERO;
    }

    private UserActivityConditionData getUserActivityConditionData(int conditionId){
        for (UserActivityConditionData userActivityConditionData : activityData.getUserActivityConditionDataList()) {
            if (userActivityConditionData.getConditionId() == conditionId) {
                return userActivityConditionData;
            }
        }
        return null;
    }

    protected abstract void player_BoardLevelHandle(Object object);

    protected final player_BoardLevelHandle handle = new player_BoardLevelHandle();

    public ActivityConditionInfo getConditionInfo() {
        return conditionInfo;
    }

    public void setPreValue(BigInteger value) {
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if(null != userActivityConditionData){
            userActivityConditionData.setPreValue(value);
        }
    }

    public void initDefaultValue(){

    }

    public  void setGetReward(boolean isGetReward){
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if(null != userActivityConditionData){
            userActivityConditionData.setGetReward(isGetReward);
        }
    }

    public void setModifyTime(long modifyTime) {
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if(null != userActivityConditionData){
            userActivityConditionData.setModifyTime(modifyTime);
        }
    }

    public void initValue() {
        if (activityData.isInTime()) {
            if(getValue().compareTo(BigInteger.ZERO) == 0) {
                initDefaultValue();
            }
        }
    }

    public class player_BoardLevelHandle implements IEventListener {
        @Override
        public void triggerEvent(EventTriggerArgs eventArgs) {
            changeValue(eventArgs.getArgs());
        }
    }

    public void changeValue(Object object) {
        if (activityData.isShowTime()) {// 活动期间改变值
            player_BoardLevelHandle(object);
        }
    }

    public int getConditionId() {
        return conditionInfo.getConditionId();
    }

    public int getConditionType() {
        return conditionInfo.getType();
    }
}
